<html>
<head><meta charset="utf-8"><title>why does mutating const only raise a warning? · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html">why does mutating const only raise a warning?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216723574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216723574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216723574">(Nov 14 2020 at 11:32)</a>:</h4>
<p>When mutating a const</p>
<div class="codehilite"><pre><span></span><code>fn main() {
    const B: String = String::new(); // Not OK
    B.push_str(&quot;asdf asdf asdf&quot;);
    println!(&quot;here {}&quot;, B);
}
</code></pre></div>
<p>the compiler only raises a warning, that mutating consts creates temporaries. I would expect it to fail. Why does this compile at all?</p>



<a name="216725808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216725808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216725808">(Nov 14 2020 at 12:14)</a>:</h4>
<p>The warning was introduced in <a href="https://github.com/rust-lang/rust/pull/75573">https://github.com/rust-lang/rust/pull/75573</a>. It works because <code>B</code> is assigned to a temporary. The <code>push_str</code> is then called on this temporary.</p>



<a name="216726265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216726265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216726265">(Nov 14 2020 at 12:27)</a>:</h4>
<p>yea, basically <code>B.push_str("asdf");</code> is <code>let mut tmp = B; tmp.push_str("asdf");</code>, which copies <code>B</code> and then mutates the copy, and then throws away the copy</p>



<a name="216727322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727322">(Nov 14 2020 at 12:45)</a>:</h4>
<p>Does <code>const</code> in rust mean something else? Isn't constant synonymous with immutable?</p>



<a name="216727389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727389">(Nov 14 2020 at 12:46)</a>:</h4>
<p>I get that the original value didn't change. But isn't it simply better to raise a compiler error when you are mutating a <code>const</code>ant?</p>



<a name="216727502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727502">(Nov 14 2020 at 12:48)</a>:</h4>
<p>well const has multiple meanings</p>



<a name="216727544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727544">(Nov 14 2020 at 12:49)</a>:</h4>
<p>the issue here is, as <span class="user-mention silent" data-user-id="133247">bjorn3</span> and <span class="user-mention silent" data-user-id="124288">oli</span> have said, that  you are mutating a temporary which is assigned a copy of the constant, not the constant itself</p>



<a name="216727617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727617">(Nov 14 2020 at 12:50)</a>:</h4>
<p>yes. i get that. But when I first did this (and ignored the compiler warning thinking it was another unused variable lint), i was super confused by the outcome</p>



<a name="216727634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727634">(Nov 14 2020 at 12:51)</a>:</h4>
<p>I did not expect it to be compilable (as I was mutating a constatnt), but the compiler didn't complain</p>



<a name="216727653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727653">(Nov 14 2020 at 12:51)</a>:</h4>
<p>yeah, iirc there are actually some valid uses for this. I do think it would make sense to increase that lint be deny by default though</p>



<a name="216727723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216727723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216727723">(Nov 14 2020 at 12:52)</a>:</h4>
<p>the lint is mostly just quite new, so starting out as <code>deny</code> would probably a bit too invasive <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> maybe with the release of the 2021 edition</p>



<a name="216729047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216729047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216729047">(Nov 14 2020 at 13:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="355545">Vishnunarayan K. I.</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F/near/216727322">said</a>:</p>
<blockquote>
<p>Does <code>const</code> in rust mean something else? Isn't constant synonymous with immutable?</p>
</blockquote>
<p><code>const</code> is a form of "inline", really -- it means "behave as if the bod of the const initializer is copy-pasted to all use sites"</p>



<a name="216729056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216729056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216729056">(Nov 14 2020 at 13:29)</a>:</h4>
<p>note that <code>let x = String::new()</code> already creates an immutable binder, but <code>const</code> is very different from <code>let</code></p>



<a name="216729152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216729152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216729152">(Nov 14 2020 at 13:31)</a>:</h4>
<p>yeah, i'm re-reading const-eval#20 and this seems to be discussed there. quite a different meaning of <code>const</code>.</p>



<a name="216824424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/why%20does%20mutating%20const%20only%20raise%20a%20warning%3F/near/216824424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/why.20does.20mutating.20const.20only.20raise.20a.20warning.3F.html#216824424">(Nov 16 2020 at 01:14)</a>:</h4>
<p><code>const B: String = String::new();</code> is essentially <code>const fn B() -&gt; String { String::new() }</code>, just in a way that it gets auto-called.</p>
<p>Basically, <code>B</code> creates a new temporary, and there are actually useful cases for mutating temporaries, so this is just an unfortunate confluence of features, thus the new warning.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>